package kr.co.graviti.adm.bbs.action;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import kr.co.graviti.adm.common.dao.AdmCommonDAO;
import kr.co.graviti.adm.common.vo.AdmBbsVO;
import kr.co.graviti.adm.common.vo.AdmBbsVOToHashMap;
import kr.co.graviti.common.action.BaseFuncAction;
import kr.co.graviti.common.util.CommonUtil;
import kr.co.graviti.common.util.FilePolicyUtil;
import kr.co.graviti.common.vo.MultipartUpLoadFileInfoVO;

public class AdmBbsInsertProcAction extends BaseFuncAction{
	
	private static final Logger _log = Logger.getLogger(AdmBbsInsertProcAction.class);
	
	public ActionForward execute(ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,HttpServletResponse response)
	        throws Exception {
		
		HashMap req = null;
		HashMap bbsSort = new HashMap();
		req = CommonUtil.getRequest(request);
		String ibatisNm = "admBbs";
		AdmCommonDAO dao = new AdmCommonDAO();
		
		/* 게시판 기본정보 */
		int bbsCnt = dao.getIntegerDAO(req, ibatisNm+".selectBbsSortCount");
		if(bbsCnt > 0){
			bbsSort = dao.getHashMapDAO(req, ibatisNm+".selectBbsSort");
			req.put("board_cd", bbsSort.get("BOARD_CD"));
		}else{
			String alertMsg = CommonUtil.printAlertBack("잘못된 페이지 정보 입니다..");	
			request.setAttribute("alertMsg", alertMsg);
			return mapping.findForward("fail");
		}
		
		AdmBbsVO vo = (AdmBbsVO)form;
		
		req = new AdmBbsVOToHashMap().bbsHashMapInfoMap(vo, request, response);
		req.put("board_cd",bbsSort.get("BOARD_CD"));
		
		
		// insert seq 생성 
		dao.setInsertDAO(ibatisNm+".commInsert");
		int attch_file_idx = dao.getIntegerDAO(ibatisNm+".commVal");
		req.put("attch_file_idx", Integer.toString(attch_file_idx));
		req.put("board_type",bbsSort.get("BOARD_TYPE"));
		dao.setInsertDAO(req, ibatisNm + ".commBoard_insert");
		
		
		// 첨부파일 이미지 저장 및 업로드 
		HashMap imgfilemap = new HashMap();
		imgfilemap.put("attch_file_idx", Integer.toString(attch_file_idx));
		List imgFile = vo.getImgFile();
		List thumFile = vo.getThumfile();
		List upfiles = vo.getUpfile();
		
		/*썸네일 업로드 */
		int thumcnt = 0;
		for(int i=0;i<thumFile.size();i++){
			if(!CommonUtil.strNullChk(thumFile.get(i).toString(),"").equals("")){
				FormFile theFile = (FormFile)thumFile.get(i);
				MultipartUpLoadFileInfoVO fileInfo = 
					new FilePolicyUtil().multiFileUpLoad(theFile, (String)bbsSort.get("BOARD_TYPE"), BBS_DEFAULT_PATH, (thumcnt+1));
				
				imgfilemap.put("org_filenm", fileInfo.getOrginalFilename());
				imgfilemap.put("chg_filenm", fileInfo.getSaveFilename());
				imgfilemap.put("filetype_cd", req.get("thumfile_type"));
				imgfilemap.put("file_size", Long.toString(fileInfo.getFilesize()));
				
				dao.setInsertDAO(imgfilemap, ibatisNm + ".fileInsert");
				thumcnt++;
			}
		}
		
		/*다중첨부 이미지 파일업로드 */
		int imgcnt = 0;
		for(int i=0;i<imgFile.size();i++){
			if(!CommonUtil.strNullChk(imgFile.get(i).toString(),"").equals("")){
				FormFile theFile = (FormFile)imgFile.get(i);
				MultipartUpLoadFileInfoVO fileInfo = 
					new FilePolicyUtil().multiFileUpLoad(theFile, (String)bbsSort.get("BOARD_TYPE"), BBS_DEFAULT_PATH, (imgcnt+1));
				imgfilemap.put("org_filenm", fileInfo.getOrginalFilename());
				imgfilemap.put("chg_filenm", fileInfo.getSaveFilename());
				imgfilemap.put("filetype_cd", req.get("imgFile_type"));
				imgfilemap.put("file_size", Long.toString(fileInfo.getFilesize()));
				
				dao.setInsertDAO(imgfilemap, ibatisNm + ".fileInsert");
				imgcnt++;
			}
		}
		
		/*첨부파일 업로드 */
		int filecnt = 0;
		for(int i=0;i<upfiles.size();i++){
			if(!CommonUtil.strNullChk(upfiles.get(i).toString(),"").equals("")){
				FormFile theFile = (FormFile)upfiles.get(i);
				MultipartUpLoadFileInfoVO fileInfo = 
					new FilePolicyUtil().multiFileUpLoad(theFile, (String)bbsSort.get("BOARD_TYPE"), BBS_DEFAULT_PATH, (filecnt+1));
				imgfilemap.put("org_filenm", fileInfo.getOrginalFilename());
				imgfilemap.put("chg_filenm", fileInfo.getSaveFilename());
				imgfilemap.put("filetype_cd", req.get("upfile_type"));
				imgfilemap.put("file_size", Long.toString(fileInfo.getFilesize()));
				dao.setInsertDAO(imgfilemap, ibatisNm + ".fileInsert");
				filecnt++;
			}
		}
		_log.info("--- insert success ----");
		request.setAttribute("req", req);
		request.setAttribute("board_type", bbsSort.get("BOARD_TYPE"));
		return mapping.findForward("success");
	}
}
